package com.example.components.exception;

import com.example.components.utils.ExceptionUtil;

import java.io.Serializable;
import java.util.Date;

/**
 * User: lu
 * Date: 2022/4/21
 * Time: 15:58
 * Version: 1.0
 * Modified By:
 * Description:
 */
public final class BaseAppException extends Exception implements Serializable {
    public static final int INNER_ERROR = 1;
    public static final int BUSS_ERROR = 0;
    private static final long serialVersionUID = -27020729327620212L;
    private int id;
    private String code;
    private String desc;
    private String localeMessage;
    private Date time;
    private int type;
    //    private LogModule logModule;
    private BaseAppException baseAppException;

    public BaseAppException() {
    }

    public BaseAppException(String code) {
        this(code, (String) null, 1, (Throwable) null);
    }

    public BaseAppException(String code, String msg) {
        this(code, msg, 1, (Throwable) null);
    }

    public BaseAppException(String code, Throwable cause) {
        this(code, (String) null, 1, cause);
    }

    public BaseAppException(String code, int errorType, Throwable cause) {
        this(code, (String) null, errorType, cause);
    }

    public BaseAppException(String code, String msg, int errorType) {
        this(code, msg, errorType, (Throwable) null);
    }

    public BaseAppException(String errorCode, String message, int errorType, Throwable cause) {
        super(message, cause);
        this.code = errorCode;
        this.desc = message;
        BaseAppException beCause = ExceptionUtil.getFirstBaseAppException(cause);
        if (beCause == null) {
            this.type = errorType;
        } else {
            this.type = beCause.getType();
        }

        if (cause instanceof BaseAppException) {
            this.baseAppException = (BaseAppException) cause;
        }

    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Date getTime() {
        if (this.time == null) {
            this.time = new Date();
        }

        return new Date(this.time.getTime());
    }

    public void setTime(Date time) {
        this.time = new Date(time.getTime());
    }

    public int getType() {
        return this.type;
    }

    public void setType(int type) {
        this.type = type;
    }

//    public LogModule getLogModule() {
//        return this.logModule;
//    }

//    public void setLogModule(LogModule logModule) {
//        this.logModule = logModule;
//    }

    public BaseAppException getBaseAppException() {
        return this.baseAppException;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("errorCode = [");
        sb.append(this.code);
        sb.append("] errorDesc = [");
        if (this.localeMessage != null) {
            sb.append(this.localeMessage);
        }

        sb.append("]");
        if (this.desc != null) {
            sb.append("  Description = [");
            sb.append(this.desc);
            sb.append("]");
        }

        return sb.toString();
    }

    public String toStringNonTrace() {
        StringBuilder sb = new StringBuilder();
        sb.append("errorCode = [");
        sb.append(this.code);
        sb.append("] errorDesc = [");
        if (this.localeMessage != null) {
            sb.append(this.localeMessage);
        }

        sb.append("]");
        if (this.desc != null) {
            sb.append("  Description = [");
            sb.append(this.desc);
            sb.append("]");
        }

        Throwable cause = this.getCause();
        if (cause != null) {
            while (cause.getCause() != null) {
                cause = cause.getCause();
            }
        }

        if (cause != null) {
            sb.append(" cause = [");
            sb.append(cause.getClass().getName());
            sb.append(":");
            sb.append(cause.getMessage());
            sb.append("]");
        }

        return sb.toString();
    }

    @Override
    public String getMessage() {
        String message = super.getMessage();
        if (message == null) {
            message = this.getLocaleMessage();
            if (this.code != null) {
                message = '[' + this.code + "] " + message;
            }
        } else if (this.code != null) {
            message = '[' + this.code + "] " + message;
        }

        return message;
    }

    public String getLocaleMessage() {
        return this.localeMessage;
    }

    public void setLocaleMessage(String localeMessage) {
        this.localeMessage = localeMessage;
    }

    public String getDetailMessage() {
        StringBuilder content = new StringBuilder();
        if (this.code != null) {
            content.append('[').append(this.code).append("] ");
        }

        if (this.localeMessage != null) {
            content.append('[').append(this.localeMessage.trim()).append("] ");
        }

        String message = super.getMessage();
        if (message != null) {
            content.append('[').append(message.trim()).append("] ");
        }

        return content.toString();
    }
}
